/*
 *  Scene.c
 *  final
 *
 *  Created by Javier Quevedo on 1/21/09.
 *  Copyright 2009 __MyCompanyName__. All rights reserved.
 *
 */

#import "Scene.h"
#import "Node.h"
#if defined(WIN32)
#include <GL/glut.h>
#else
#include <GLUT/glut.h>
#endif



Scene *initScene(){
	Scene *newScene = malloc(sizeof(Scene));
	newScene->nodeCount=0;
	newScene->sceneNodes= malloc(sizeof(Node));
	return newScene;
}

void addNodeToScene(Scene *scene, Node *node){
	scene->nodeCount++;
	scene->sceneNodes = realloc(scene->sceneNodes, (sizeof(Node)*scene->nodeCount));
	scene->sceneNodes[scene->nodeCount-1] = node;
	
}


void removeNode(Scene *scene, Node *node){
	int i =0;
	for (i = 0; i < scene->nodeCount; i++)
	{
			if (scene->sceneNodes[i] == node)
			{
				setAsLastNode(scene, i);
				scene->sceneNodes[scene->nodeCount-1] = NULL;
				scene->sceneNodes--;
			}
	}
}

void setAsLastNode(Scene *scene, int nodeNumber)
{
	if (nodeNumber+1 == scene->nodeCount)
		return;
	
	Node *swapNode = scene->sceneNodes[nodeNumber];
	scene->sceneNodes[nodeNumber] = scene->sceneNodes[scene->nodeCount-1];
	scene->sceneNodes[scene->nodeCount-1] = swapNode;
	return;
	
}

void renderScene(Scene *scene){
	int i;
	for (i = 0; i < scene->nodeCount; i++)
	{
		renderNode(scene->sceneNodes[i]);
	}
}

void releaseScene(Scene *scene){
	int i;
	printf ("releasing scene");
	for (i = 0; i,scene->nodeCount; i++)
	{
		releaseNode(scene->sceneNodes[i]);
	}
	free(scene);
}
